//优化根路径
axios.defaults.baseURL = `http://ajax-api.itheima.net/`


//优化头部
axios.interceptors.request.use((config) => {
  // console.log(config);

  // indexof返回的-1表示"找不到匹配项"，用的是其索引
  // if (config.url.indexof(`/my`) !== -1) {
  //   config.headers.Authorization=localStorage.getItem(`token`);
  // }


  if (localStorage.getItem(`token`)) {
    config.headers.Authorization = localStorage.getItem(`token`);
  }

  return config;
}, error => {

  return Promise.reject(error);
});


//响应拦截器
axios.interceptors.response.use((response) => {

  return response;
},
  (error) => {
    if (error.response.status === 401) {
      localStorage.removeItem(`token`)
      localStorage.removeItem(`username`)
      location.href = `./login.html`
    }

    return Promise.reject(error);
  });